package com.agileboot.domain.app.application;

import cn.hutool.core.date.DateUtil;
import cn.hutool.core.util.StrUtil;
import com.agileboot.common.enums.common.LoanApplicationStatusEnum;
import com.agileboot.domain.app.application.command.AppLoanApplicationDTO;
import com.agileboot.domain.app.application.model.LoanModel;
import com.agileboot.domain.app.application.model.LoanModelFactory;
import com.agileboot.infrastructure.user.AuthenticationUtils;
import com.agileboot.infrastructure.user.app.AppLoginUser;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;

@Service
@RequiredArgsConstructor
public class LoanApplicationService {

    // #################### 常见错误: Not Found TableInfoCache ###################
    // 是因为mapper 文件没有加入spring 管理，找不到 mapper 文件，就会找不到 model
    // 解决方法： 在 mapper 上加上 @mapper 或者在启动类加上@mapperscan
    // 参考： https://zhuanlan.zhihu.com/p/509491403


    private final LoanModelFactory loanModelFactory;

    /**
     * 申请贷款
     *
     * @param command
     */
    public void addLoanApplication(AppLoanApplicationDTO command) {

        // 默认贷款申请参数
        if (StrUtil.isNotEmpty(command.getProductId())) {
            command.setProductId("1");
        }

        AppLoginUser appLoginUser = AuthenticationUtils.getAppLoginUser();
        LoanModel loanModel = loanModelFactory.create();

        loanModel.copyProperties(command);
        loanModel.setApprovalStatus(LoanApplicationStatusEnum.WAITING_APPROVAL);
        loanModel.setUserId(appLoginUser.getUserId());
        loanModel.setApplicationDate(DateUtil.date());
        loanModel.insert();
    }


    /**
     * 计算信用分
     */
    public void calculateLoan() {
        // 假设的信用历史分数计算参数

        double income = 5000; // 假设收入为5000美元
        double totalLiabilities = 1000; // 假设总负债为1000美元

        //  请帮我设计一个spring boot 应用的 个人信用贷计算器,用于计算个人信用贷的额度、如果有补充的功能，请帮我一并完成
    }
}
